package com.springboot.demo.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.demo.entity.Notice;
import com.springboot.demo.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping()
    public String publish(){
        return "notice";
    }


    //转到公告页面
    @RequestMapping("/to")
    public String to(Model model){
        List<Notice> noteList = noticeService.getNoticeByOrder();
        model.addAttribute("notices",noteList);
        return "noticetest";
    }


    //增加公告信息
    @RequestMapping("/add")
    public String add(
            @RequestParam("title") String title,
            @RequestParam("detail") String detail,
            Model model
    ){
        model.addAttribute("title",title);
        model.addAttribute("detail",detail);

        if(title==null || title==""){
            model.addAttribute("error","标题不能为空！");
            return "notice";
        }
        if(detail==null || detail==""){
            model.addAttribute("error","公告内容不能为空！");
            return "notice";
        }
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setDetail(detail);
        notice.setDate(new Date());
        //System.out.println(notice);
        noticeService.save(notice);
        return "redirect:/librarian_book";
    }

    //获取最近的公告信息
    @RequestMapping("/update")
    public String getLastNotice(
            HttpSession session
    ){
        String date = noticeService.getLastDate();
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.eq("date",date);
        Notice queryNotice = noticeService.getOne(noticeQueryWrapper);
        session.setAttribute("notice",queryNotice);
        return "notice_update";
    }

    //更改公告信息
    @RequestMapping("/updatenotice")
    public String updateNotice(
            @RequestParam("title") String title,
            @RequestParam("detail") String detail,
            HttpSession session,
            Model model
    ){
        model.addAttribute("title",title);
        model.addAttribute("detail",detail);
        if(title==null || title==""){
            model.addAttribute("error","标题不能为空！");
            return "notice";
        }
        if(detail==null || detail==""){
            model.addAttribute("error","公告内容不能为空！");
            return "notice";
        }
        Notice notice = (Notice) session.getAttribute("notice");
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.eq("title",notice.getTitle());
        noticeQueryWrapper.eq("detail",notice.getDetail());
        Notice queryNotice = noticeService.getOne(noticeQueryWrapper);
        notice.setTitle(title);
        notice.setDate(new Date());
        notice.setDetail(detail);
        noticeService.remove(noticeQueryWrapper);   //移除之前的公告
        noticeService.save(notice);    //保存更改后的公告
        return "redirect:/librarian_book";
    }

    //删除公告信息
    @RequestMapping("/delete")
    public String deleteNotice(
            HttpSession session
    ){
        Notice notice = (Notice) session.getAttribute("notice");
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.eq("title",notice.getTitle());
        noticeQueryWrapper.eq("detail",notice.getDetail());
        Notice queryNotice = noticeService.getOne(noticeQueryWrapper);
        noticeService.remove(noticeQueryWrapper);
        return "redirect:/librarian_book";
    }

}
